package cmd

import (
	"daemon/process"
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

var proc = process.NewProcess(&DemoServe{})
var Command = &cobra.Command{}

func init() {
	file, err := os.Create("./daemon.log")
	if err != nil {
		panic(err)
	}
	proc.SetPipeline(nil, file, file)
	Command.CompletionOptions.DisableDefaultCmd = true
	Command.AddCommand(start)
	Command.AddCommand(stop)
	Command.AddCommand(restart)
	s := "重启程序\n\n语法:\n  restart [参数]\n\n参数:\n  -h  帮助\n\n"
	restart.SetHelpTemplate(s)
}

var start = &cobra.Command{
	Use:   "start",
	Short: "start process",
	Run: func(cmd *cobra.Command, args []string) {
		err := proc.Start()
		if err != nil {
			panic(err)
		}
		fmt.Println("start...")
	},
}
var stop = &cobra.Command{
	Use:   "stop",
	Short: "stop process",
	Run: func(cmd *cobra.Command, args []string) {
		err := proc.Stop()
		if err != nil {
			panic(err)
		}
		fmt.Println("stop...")
	},
}
var restart = &cobra.Command{
	Use:   "restart",
	Short: "Restart Process",
	Run: func(cmd *cobra.Command, args []string) {
		err := proc.Restart()
		if err != nil {
			panic(err)
		}
		fmt.Println("restart...")
	},
}
